package cn.fbs.controller;

import cn.fbs.model.TAtlasInfo;
import cn.fbs.service.PhotoAlbumService;
import cn.fbs.service.UploadService;
import cn.fbs.vo.TAtlasInfoUpdateVo;
import cn.fbs.vo.TAtlasInfoVo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * @author 安逸
 * @date 2021/1/26 - 19:33
 **/

@Slf4j
@Api(tags = "图册模块")
@RestController
@RequestMapping("photoAlbum")
public class PhotoAlbumController {
    @Value("${resource.locations}")
    private String resourceLocations;
    @Autowired
    private PhotoAlbumService photoAlbumService;

    @Autowired
    private UploadService uploadService;



    /**
     * 查图册
     */
    @ApiOperation("根据id查询图册,可以输入多个")
    @PostMapping("/queryPhotoInfoById")
    public List<TAtlasInfo> queryPhotoInByIds(@RequestBody List<Integer> Ids) {
         System.out.println("queryPhotoInByIds");
        List<TAtlasInfo> tAtlasInfos = photoAlbumService.queryPhotoInByIds(Ids);
        return tAtlasInfos;
    }


    @ApiOperation("根据名称查询图册")
    @GetMapping("/queryPhotoInfoByName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "输入图册的名字", required = false)
    })
    public List<TAtlasInfo> queryPhotoInByName(String name) {
        return photoAlbumService.queryPhotoInByName(name);
    }

    /**
     * 新增图册
     */
    @ApiOperation("新增图册")
    @PostMapping("/savePhotoAlbum")
    public String savePhotoAlbum(@RequestBody TAtlasInfoVo infoVo, HttpServletRequest request) {

        String realFileUrl = infoVo.getFileUrl();
        if (StringUtils.isBlank(realFileUrl)) {
            return "500,文件地址无效";
        }
        if (!new File(realFileUrl).exists()) {
            return "500,文件地址无效";
        }
        String newUrl = realFileUrl.replaceFirst(resourceLocations, "");
        infoVo.setFileUrl(newUrl);
        TAtlasInfo tAtlasInfo = new TAtlasInfo();
        BeanUtils.copyProperties(infoVo, tAtlasInfo);
        tAtlasInfo.setOperator(getUserName(request));
        photoAlbumService.savePhotoAlbum(tAtlasInfo);
        return "新增成功";
    }
    private String getUserName(HttpServletRequest request){
        String sessionId = request.getHeader("sessionId");
        sessionId = new String(Base64.getDecoder().decode(sessionId));
        JSONObject jsonObject = JSONObject.parseObject(sessionId);
        String userName = jsonObject.get("userName").toString();
        return userName;
    }
    /**
     * 编辑图册信息
     */
    @ApiOperation("编辑图册信息")
    @PostMapping("/updatePhotoAlbum")
    public String updatePhotoAlbum(@RequestBody TAtlasInfoUpdateVo updateVo) throws Exception {
         System.out.println("updatePhotoAlbum");
        TAtlasInfo tAtlasInfo = new TAtlasInfo();
        BeanUtils.copyProperties(updateVo, tAtlasInfo);
        photoAlbumService.updatePhotoAlbum(tAtlasInfo);
        return "200,上传成功";
    }

    /**
     * 删除图册信息
     */
    @ApiOperation("删除图册信息")
    @GetMapping("/deletePhotoAlbum")
    @ApiImplicitParam(name = "id", value = "图册ID", required = true)
    public ResponseEntity<Map<String, Object>> deletePhotoAlbum(@RequestParam("id") String id) {
         System.out.println("deletePhotoAlbum");  return ResponseEntity.ok(photoAlbumService.deletePhotoAlbum(id));
    }


}
